跳到主要内容

MyBatisPlus 的配置文件

前置准备

MyBatisPlus 的官网 出现问题可以看 仓库的 issue

配置环境

引入 mybatis-plus 不用再添加 MyBatis 的依赖,不然可能有版本错误

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>

application.yml 配置

# DataSource Config
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/studyPlus?useUnicode=true&characterEncoding=utf8&useSSL=true&useServerPrepStmts=true
username: root
password: root

在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹(或直接用 @Mapper):

@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(QuickStartApplication.class, args);
}
}

官方的 Demo

创建一个实体类

@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}

创建一个 Mapper,注意这里继承了 BaseMapper 这个类

public interface UserMapper extends BaseMapper<User> {
}

然后就能直接使用了

@SpringBootTest
class StudymybatisplusApplicationTests {

@Autowired
private UserMapper userMapper;

@Test
void contextLoads() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
userList.forEach(System.out::println);
}

}

常用注解

@TableId

id 插入策略可以自行更改,只需在 Entity 对象上加个注解就行了(这个 IdType 是个枚举类型,里面还有很多其它策略)

// SQL 修改自增 ID 起始值 alter table users AUTO_INCREMENT=10000;

@Data
public class User {
// 设置 id 为自增长(表的相应字段必须也设置成自增长)
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
}

@TableName

加在 Entity 上面,表示这个 Entity 对应的表名

@Data
@TableName("tb_user")
public class User {
...
}

@TableField

参考资料 官方文档 @TableField

可以通过这个 @TableField 注解指定字段的一些属性,常常用于解决下面几个问题 1、对象中的属性名和字段名不一致的问题 2、对象中的属性字段在表中不存在的问题 3、想隐藏某些字段(例如密码不查询出来) 4、其它功能看文档...

@Data
@TableName("tb_user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
@TableField(select = false) // 查询时不返回该字段
private String password;
private String name;
private Integer age;
@TableField(value = "email") // 解决字段名不一致的问题
private String mail;
@TableField(exist = false) // 解决字段不存在的问题
private String address;
}

如果表中不存在的字段还插入进去就会报错

配置文件

因为 官方文档 已经很棒了,所以这里只介绍一些常用的配置

configLocation

MyBatis 配置文件位置,如果有单独的 MyBatis 配置,要将其路径配置到 configLocation

mybatis-plus:
config-location: classpath:mybatis-config.xml

注意:使用了 configuration 下面的配置就不能再设置这个,否则两个配置文件会冲突报错

mapperLocations

MyBatis Mapper 所对应的 XML 文件位置

# classpath 后面跟个 * 表示会扫描所有依赖下的 mybatis 里的 .xml 文件
# 没有跟个 * 就只扫描当前的项目的
mybatis-plus:
mapper-locations: classpath*:mybatis/*.xml

别名包扫描路径

别名包扫描路径,通过该属性可以给包中的类注册别名,注册后在 Mapper 对应的 XML 文件中可以直接使用类名,而不用使用全限定的类名

mybatis-plus:
type-aliases-package: com.alsritter.pojo

自动驼峰命名规则

是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射。(此属性在 MyBatis 中原默认值为 false,在 MyBatis-Plus 中,此属性也将用于生成最终的 SQL 的 select body)

不过一般都是使用下划线,所以不用开启这个

mybatis-plus:
configuration:
mapUnderscoreToCamelCase: true

开启缓存

开启 Mybatis 二级缓存,默认为 true

mybatis-plus:
configuration:
cache-enabled: true

DB 策略配置

例如上面的那个 @TableId 注解就是一个 DB 策略

1、IdType:默认主键生成策略

mybatis-plus:
global-config:
db-config:
id-type: auto

2、tablePrefix:表名前缀,全局配置后可以省略 @TableName 配置

mybatis-plus:
global-config:
db-config:
table-prefix: tb_

就是会默认把 Entity 对象的名称首字母小写与这个前缀拼接成这个表的表名:User --> tb_user